package com.ruoyi.project.charge.service.impl;

import com.ruoyi.project.charge.domin.*;
import com.ruoyi.project.charge.mapper.ChargeMapper;
import com.ruoyi.project.charge.service.IChargeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.UUID;

/**
 * @auther:YU JIANG
 * @date:2021/7/13
 * @time:14:37
 */
@Service
public class ChargeServiceImpl implements IChargeService {

    @Autowired
    private ChargeMapper chargeMapper;

    @Override
    @Transactional
    public List<ChargeInfo> queryInfo(String reg_id) {
        List<ChargeInfo> chargeInfoList = chargeMapper.selectInfo(reg_id);
        List<ChargeMedInfo> chargeMedInfos = chargeMapper.selectMedInfo(reg_id);
        List<ChargeProjectInfo> chargeProjectInfos = chargeMapper.selectProjectInfo(reg_id);
        if (chargeInfoList!=null){
            ChargeInfo chargeInfo = chargeInfoList.get(0);
            chargeInfo.setChargeMedInfos(chargeMedInfos);
            chargeInfoList.add(0,chargeInfo);
        }
        if (chargeProjectInfos!=null){
            ChargeInfo chargeInfo = chargeInfoList.get(0);
            chargeInfo.setChargeProjectInfos(chargeProjectInfos);
            chargeInfoList.add(chargeInfo);
        }
        return chargeInfoList;
    }

    @Override
    @Transactional
    public int updatePayStatus(String reg_id, String pay_status, List<String> check_id, List<String> drug_id,String total_price,String payment_method,List<String> drug_amount,List<String> drug_name,List<String> drug_Rx_price,List<String> drug_price,String drug_type,String exam_type,List<String> exam_name,List<String> exam_id,List<String> case_price) {
        List<ChargeProjectInfo> chargeProjectInfos = chargeMapper.selectProjectInfo(reg_id);
        ChargeProjectInfo chargeProjectInfo = chargeProjectInfos.get(0);
        String case_id = chargeProjectInfo.getCase_id();
        System.out.println("病例ID："+case_id);
        /*修改支付状态*/
        int i = 1;
        if (check_id!=null) {
            for (String id : check_id) {
                try {
                    chargeMapper.updateProPay(case_id,id,pay_status);
                } catch (Exception e) {
                    i=0;
                }
            }
        }

        if (drug_id!=null) {
            for (String id : drug_id) {
                try {
                    chargeMapper.updateMedPay(reg_id,id,pay_status);
                } catch (Exception e) {
                    i=0;
                }
            }
        }
        if (pay_status.equals("1")) {
            /*创建订单*/
            List<ChargeMedInfo> chargeMedInfos = chargeMapper.selectMedInfo(reg_id);
            Charge charge = new Charge();
            String uuid = UUID.randomUUID().toString();
            String s = uuid.replaceAll("-", "");
            charge.setOrder_id(s);
            charge.setRegistration_id(reg_id);
            List<ChargeInfo> chargeInfos = chargeMapper.selectInfo(reg_id);
            ChargeInfo chargeInfo = chargeInfos.get(0);
            String pati_name = chargeInfo.getPati_name();
            charge.setPatient_name(pati_name);
            //总金额
            charge.setTotal_amount(total_price);
            charge.setPayment_method(payment_method);
            charge.setOrder_status(pay_status);

            try {
                chargeMapper.insertCharge(charge);
            } catch (Exception e) {
                i=0;
            }

            ChargeDetail chargeDetail = new ChargeDetail();
            try {
                //创建药品详情订单
                chargeDetail.setOrder_id(s);
                System.out.println("单号："+s);
                chargeDetail.setType(drug_type);
                System.out.println("类型："+drug_type);
                chargeDetail.setStatus(pay_status);
                System.out.println("状态："+pay_status);

                if (drug_id!=null && drug_price!=null &&drug_amount!=null && drug_name!=null) {
                    for (int j = 0; j < drug_id.size()||j<drug_price.size()||j<drug_amount.size()||j<drug_name.size(); j++) {
                        chargeDetail.setDrug_name(drug_name.get(j));
                        chargeDetail.setDrug_id(drug_id.get(j));
                        chargeDetail.setDrug_price(drug_price.get(j));
                        chargeDetail.setDrug_amount(drug_amount.get(j));
                        chargeDetail.setDrug_Rx_price(drug_Rx_price.get(j));
                        System.out.println("药品名称："+drug_name.get(j)+",药品ID："+drug_id.get(j)+",药品单价:"+drug_Rx_price.get(j)+",药品价格："+drug_price.get(j)+",药品数量："+drug_amount.get(j));
                        chargeMapper.insertChargeDetail(chargeDetail);
                    }
                }
            } catch (Exception e) {
                i=0;
            }

            try {
                //创建项目详情订单
                chargeDetail.setOrder_id(s);
                System.out.println("单号："+s);
                chargeDetail.setType(exam_type);
                System.out.println("类型："+exam_type);
                chargeDetail.setStatus(pay_status);
                System.out.println("状态："+pay_status);
                chargeDetail.setDrug_amount("1");
                System.out.println("数量："+1);
                if (exam_name!=null && exam_id!=null && case_price!=null) {
                    for (int j = 0; j < exam_name.size()||j < exam_id.size()||j < case_price.size(); j++) {
                        chargeDetail.setDrug_name(exam_name.get(j));
                        chargeDetail.setDrug_id(exam_id.get(j));
                        chargeDetail.setDrug_price(case_price.get(j));
                        chargeDetail.setDrug_Rx_price(case_price.get(j));
                        System.out.println("项目名称："+exam_name.get(j)+",项目ID："+exam_id.get(j)+",项目价格："+case_price.get(j));
                        chargeMapper.insertChargeDetail(chargeDetail);
                    }
                }
            } catch (Exception e) {
                i=0;
            }
        }

        return i;
    }
}
